package states 
{
	import com.greensock.easing.Circ;
	import com.greensock.easing.Cubic;
	import com.greensock.easing.Linear;
	import com.greensock.easing.Quad;
	import com.greensock.TweenLite;
	import flash.geom.Rectangle;
	import org.despair2D.control.DelayManager;
	import org.despair2D.control.KeyboardManager;
	import org.despair2D.ui.Fusion;
	import org.despair2D.ui.puppet.ShapePuppet;
	import org.despair2D.ui.ScrollPane;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	
	import assets.AssetsUI;
	
	import org.despair2D.control.TouchManager;
	import org.despair2D.control.ZMouseEvent;
	import org.despair2D.ui.DespairUI;
	import org.despair2D.ui.LabelButton;
	import org.despair2D.ui.UIState;
	import org.despair2D.ui.events.ManipulateEvent;
	import org.despair2D.ui.events.PanelEvent;
	import org.despair2D.ui.layout.SpaceType;
	import org.despair2D.ui.puppet.DisplayObjectContainerPuppet;
	import org.despair2D.ui.puppet.ImagePuppet;
	

public class ImageState extends UIState
{
	
	private var numImage:int
	private const gapX:int = 150
	private const gapY:int = 100;
	
	private var imageList:Array = []
	private var urlList:Array = ['http://imgsrc.baidu.com/baike/abpic/item/a6c7d717e8257737c93d6dfe.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/a75fb6d3bccd1c74960a1637.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/b7bc4c66a9e7466baa184ce5.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/b840549012e67fb5a877a43b.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/8367d1fcefc971d6b801a011.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/14ce36d3d539b60060483bf4e950352ac75cb7ea.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/023b5bb5c9ea15cef137444bb6003af33b87b293.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/21e55823a69ae81d9822ed9a.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/310f3b1f6ea679fda7866987.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/389aa8fd9eca2b4208244d26.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/3a86813dc88dac68baa1679e.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/3b6833f56afd6076bc3109fc.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/3bc6f750ce96897e1138c244.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/4075890a88b5cc2994ca6b17.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/43e6c733fb787317ac4b5fe6.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/4d4970061b2a0f430308815c.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/6a22e824650029678744f991.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/6f4703951bcef95c7af4802f.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/9c57e3fadb66f48db48f31af.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/9dc3cf580d9ebfe1800a18a7.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/9f1011b30d3092f2d9335ace.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/a54e55fbb5983b3f024f5675.jpg',
								 'http://imgsrc.baidu.com/baike/abpic/item/a583631e869f64c31bd57698.jpg',
								 //'http://imgsrc.baidu.com/baike/abpic/item/a6c7d717e8257737c93d6dfe.jpg',
								 'http://imda9f1f4b8961f17a21a.jpg'
								]
	
	override public function enter( stateArgs:Array ):void
	{
		this.fusion.move(100,10)
		
		var IP:ImagePuppet
		var i:int
		var label:LabelButton
		var shape:ShapePuppet
		var correctionX:Number, correctionY:Number
		var F:Fusion
	
		numImage = urlList.length
		
		label = new LabelButton('Btn_A', 'load images')
		label.addEventListener(ManipulateEvent.CLICK, onLoadImage)
		this.fusion.addElement(label, 30, 30)
		
		label = new LabelButton('Btn_A', 'Empty')
		label.addEventListener(ManipulateEvent.CLICK, onEmpty)
		this.fusion.addElement(label, 0, 0, SpaceType.A_TOP | SpaceType.A_RIGHT)
		
		shape = new ShapePuppet()
		shape.graphics.beginFill(0xdd4444, 0.4)
		shape.graphics.drawRect(0, 0, 400, 400)
		this.fusion.addElement(shape, 200,80)
		
		mPane = new ScrollPane(400, 400)
		mPane.limitBottom = true
		mPane.horizDisableOffset = Infinity
		mPane.vertiDisableOffset = 4
		this.fusion.addElement(mPane, 200, 80)
		mPane.contentWidth = 740
		mPane.contentHeight = 600
		mPane.addEventListener(ManipulateEvent.START_DRAG, function(e:ManipulateEvent):void
		{
			if (!DespairUI.interactive)
			{
				TweenLite.killTweensOf(mPane)
				//trace('interactive false')
			}
		})
		mPane.addEventListener(ManipulateEvent.STOP_DRAG, function(e:ManipulateEvent):void
		{
			correctionX = mPane.horizCorrection
			correctionY = mPane.vertiCorrection
			if (correctionX != 0 || correctionY != 0)
			{
				DespairUI.interactive = false
				//trace('interactive false')
				TweenLite.to(mPane, 0.6, { x:mPane.x + correctionX,
										y:mPane.y + correctionY, 
										ease:Quad.easeOut, 
										onComplete:function():void
				{
					DespairUI.interactive = true
					//trace('interactive true')
				}})
			}
		})
		F = mPane.getHorizThumb('scroll', 400, 10)
		this.fusion.addElement(F, 200, 480)
		F = mPane.getVertiThumb('scroll', 400, 10)
		this.fusion.addElement(F, 600, 80)
		
		while (i < numImage)
		{
			IP = new ImagePuppet(5)
			IP.embed(AssetsUI.AT_defaultImg)
			mPane.addElement(IP, (i % 5) * gapX + IP.width / 2, int(i / 5) * gapY + IP.height / 2)
			IP.addEventListener(ManipulateEvent.CLICK, function(e:ManipulateEvent):void
			{
				trace(e.target.name)
			})
			imageList[i] = IP
			IP.name = urlList[i]
			i++
		}
		
		//this.fusion.filters = [new BlurFilter(10,10,3)]
		
		KeyboardManager.getInstance().getState().addPressListener('Q', function():void
		{
			mPane.horizRatio = 0
			mPane.vertiRatio = 0
		})
		KeyboardManager.getInstance().getState().addPressListener('E', function():void
		{
			mPane.horizRatio = 1
			mPane.vertiRatio = 0
		})
		KeyboardManager.getInstance().getState().addPressListener('A', function():void
		{
			mPane.horizRatio = 0
			mPane.vertiRatio = 1
		})
		KeyboardManager.getInstance().getState().addPressListener('D', function():void
		{
			mPane.horizRatio = 1
			mPane.vertiRatio = 1
		})
		KeyboardManager.getInstance().getState().addPressListener('D', function():void
		{
			mPane.horizRatio = 1
			mPane.vertiRatio = 1
		})
		KeyboardManager.getInstance().getState().addPressListener('L', function():void
		{
			mPane.locked = !mPane.locked
		})
	}
	

	override public function exit():void
	{
		KeyboardManager.getInstance().getState().removeAllPressListeners('Q')
		KeyboardManager.getInstance().getState().removeAllPressListeners('E')
		KeyboardManager.getInstance().getState().removeAllPressListeners('A')
		KeyboardManager.getInstance().getState().removeAllPressListeners('D')
		KeyboardManager.getInstance().getState().removeAllPressListeners('L')
		
		this.fusion.displayObject.scrollRect = null
		TweenLite.killTweensOf(mPane)
		
		var IP:ImagePuppet
		var i:int
		while (i < numImage)
		{
			IP = imageList[i];
			TweenLite.killTweensOf(IP)
			i++
		}
		imageList = null
		urlList = null
	}
	
	
	private var mPane:ScrollPane
	
	private var mTweening:Boolean

	
	private function onLoadImage(e:ManipulateEvent):void
	{
		var IP:ImagePuppet
		var i:int
		while (i < numImage)
		{
			if (i == 23)
			{
				trace(23)
			}
			
			IP = imageList[i];
			IP.load(urlList[i], false)
			IP.addEventListener(Event.COMPLETE, onScale)
			i++
		}
		
	}
	
	private function onScale(e:Event):void
	{
		var IP:ImagePuppet
		
		IP = e.target as ImagePuppet
		IP.scaleX = IP.scaleY = 0.1
		TweenLite.to(IP, 2,{scaleX:1,scaleY:1})
	}
	
	private function onEmpty(e:ManipulateEvent):void
	{
		var IP:ImagePuppet
		var i:int
		while (i < numImage)
		{
			IP = imageList[i];
			IP.embed(AssetsUI.AT_defaultImg)
			TweenLite.killTweensOf(IP, true)
			i++
		}
	}
}
}